<template>
  <h1>EL-员工列表作业</h1>
  <el-form label-width="80px" style="width: 500px;margin: 0 auto">
    <el-form-item label="姓名">
      <el-input placeholder="请输入姓名" v-model="user.name"></el-input>
    </el-form-item>
    <el-form-item label="薪资">
      <el-input placeholder="请输入薪资" v-model="user.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入岗位" v-model="user.post"></el-input>
    </el-form-item>
    <el-form-item label-width="45px">
      <el-button type="success" @click="add()">点我提交</el-button>
    </el-form-item>
  </el-form>


  <el-table :data="arr" style="width: 50%; margin: 0 auto">
    <el-table-column label="编号" type="index" width="80px"></el-table-column>
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="salary" label="工资"></el-table-column>
    <el-table-column prop="post" label="岗位"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <hr>

  <el-table :data="arr" style="width: 50%; margin: 0 auto">
    <!--    <el-table-column label="编号" type="index" width="80px"></el-table-column>-->
    <!--    <el-table-column prop="name" label="姓名"></el-table-column>-->
    <!--    <el-table-column prop="salary" label="工资"></el-table-column>-->
    <!--    <el-table-column prop="post" label="岗位"></el-table-column>-->
    <el-table-column v-for="col in tableColumns" v-bind="col"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>


<script setup>
import {ref} from "vue";

const user = ref({
  name: '',
  salary: '',
  post: ''
});

const arr = ref([]);
const add = () => {
  // console.log(user.value)
  arr.value.push({...user.value});

  // 清空user对象的数据
  user.value = {
    name: '',
    salary: '',
    post: ''
  };
}
//  删除一行数据
const del = (index, emp) => {
  if (confirm("您确认要删除" + emp.name + "吗?")) {
    arr.value.splice(index, 1);
  }
};

const tableColumns = ref([
  {type: 'index', label: '编号', width: 80},
  {label: '姓名', prop: 'name'},
  {label: '工资', prop: 'salary'},
  {label: '岗位', prop: 'post'},
])

</script>

<style scoped>

</style>